package com.prs.crm.action.oa;

import java.io.File;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.annotation.FlowActive;
import com.prs.crm.annotation.FlowStart;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.oa.Knowledge;
import com.prs.crm.domain.oa.KnowledgeType;
import com.prs.crm.exception.InValidException;
import com.prs.crm.service.oa.KnowledgeService;
import com.prs.crm.service.oa.KnowledgeTypeService;

public class KnowledgeAction extends FlowAction<Knowledge> {

	private static final long serialVersionUID = 8689785016656521988L;

	private Knowledge knowledge;

	private KnowledgeService knowledgeService;

	private PaginationSupport pagination;

	private File attach;

	private String attachFileName;

	private Integer[] knowledgeIds;

	private List<KnowledgeType> types;
	private KnowledgeTypeService knowledgeTypeService;
	private String reg;

	public List<KnowledgeType> getTypes() {
		return types;
	}

	public void setTypes(List<KnowledgeType> types) {
		this.types = types;
	}

	public KnowledgeTypeService getKnowledgeTypeService() {
		return knowledgeTypeService;
	}

	public void setKnowledgeTypeService(
			KnowledgeTypeService knowledgeTypeService) {
		this.knowledgeTypeService = knowledgeTypeService;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public Knowledge getKnowledge() {
		return knowledge;
	}

	public void setKnowledge(Knowledge knowledge) {
		this.knowledge = knowledge;
	}

	public KnowledgeService getKnowledgeService() {
		return knowledgeService;
	}

	public void setKnowledgeService(KnowledgeService knowledgeService) {
		this.knowledgeService = knowledgeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getKnowledgeIds() {
		return knowledgeIds;
	}

	public void setKnowledgeIds(Integer[] knowledgeIds) {
		this.knowledgeIds = knowledgeIds;
	}

	public void setAttach(File attach) {
		this.attach = attach;
	}

	public File getAttach() {
		return attach;
	}

	public void setAttachFileName(String attachFileName) {
		this.attachFileName = attachFileName;
	}

	public String getAttachFileName() {
		return attachFileName;
	}

	@Action("listKnowledge")
	public String execute() {
		return SUCCESS;
	}

	@Action("listKnowledgeByReg")
	public String listKnowledgeByReg() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Knowledge.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		if (reg != null) {
			if (!"all".equals(reg)) {
				reg = reg.split(",")[0];
				if (ToolKits.isInteger(reg)) {
					criteria.createAlias("type", "ty");
					criteria.add(Restrictions
							.eq("ty.id", Integer.parseInt(reg)));
				}
			}
		}
		this.setPagination(this.getKnowledgeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("alterKnowledge")
	public String alterKnowledge() {
		return SUCCESS;
	}

	@Action("alterKnowledgeByReg")
	public String alterKnowledgeByReg() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Knowledge.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		if (reg != null) {
			if (!"all".equals(reg)) {
				reg = reg.split(",")[0];
				if (ToolKits.isInteger(reg)) {
					criteria.createAlias("type", "ty");
					criteria.add(Restrictions
							.eq("ty.id", Integer.parseInt(reg)));
				}
			}
		}
		this.setPagination(this.getKnowledgeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewKnowledge")
	public String viewKnowledge() {
		return SUCCESS;
	}

	@FlowActive(target = "knowledge", flowservice = "knowledgeService")
	@Action("editKnowledge")
	public String edit() {
		this.types = this.getKnowledgeTypeService().getAllKnowledgeType();
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@FlowStart(target = "knowledge", flowservice = "knowledgeService")
	@Action(value = "saveKnowledge", results = {
			@Result(name = "success", type = "redirect", location = "listKnowledge"),
			@Result(name = "error", type = "chain", location = "editKnowledge"),
			@Result(name = "none", type = "redirect", location = "alterKnowledge") })
	public String save() {
		if (knowledge.getTitle() == null
				|| "".equals(knowledge.getTitle().trim())) {
			addActionError("标题不能为空");
			return ERROR;
		}
		if (knowledge.getType() == null) {
			addActionError("类型不能为空");
			return ERROR;

		}
		if (knowledge.getCreattime() == null) {
			addActionError("提交时间不能为空");
			return ERROR;

		}
		if (knowledge.getId() == null) {
			try {
				this.getKnowledgeService().save(knowledge, attach,
						attachFileName);
				this.setPrompt("知识提交成功");
				return SUCCESS;
			} catch (InValidException e) {
				this.addActionError(e.getMessage());
				return ERROR;
			}
		} else {
			this.getKnowledgeService().save(knowledge, attach, attachFileName);
			this.setPrompt("知识修改成功");
			return NONE;
		}
	}

	@Action(value = "removeKnowledge", results = {
			@Result(name = "success", type = "redirect", location = "alterKnowledge"),
			@Result(name = "error", type = "chain", location = "alterKnowledge") })
	public String remove() {
		if (knowledgeIds == null) {
			this.addActionError("请选择要删除的知识");
			return ERROR;
		}
		this.getKnowledgeService().batchRemove(knowledgeIds);
		return SUCCESS;
	}

	public String getReg() {
		return reg;
	}

	public void setReg(String reg) {
		this.reg = reg;
	}

}
